package com.itheima.service.db.Impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.domain.vo.AnalysisSummaryVo;
import com.itheima.mapper.db.AnalysisByDayMapper;
import com.itheima.mapper.db.LogMapper;
import com.itheima.util.ComputeUtil;
import com.itheimma.service.db.AnalysisByDayService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;

import java.math.BigDecimal;
import java.util.Date;

@DubboService
public class AnalysisByDayServiceImpl implements AnalysisByDayService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public void LogToAnalysisByDay() {
        //准备基本数据
        //1-1今天时间
        String today = DateUtil.offsetDay(new Date(), 0).toString();
        //1-2昨天时间
        String yesterday = DateUtil.offsetDay(new Date(), -1).toString();
        //查询今日数据
        //2-1查询今日注册
        Integer numRegistered = logMapper.findTypeAndTime("0102", today);
        //2-2查询今日活跃
        Integer numActive = logMapper.findByTime(today);
        //2-3查询登入次数
        Integer numLogin = logMapper.findTypeAndTime("0101", today);
        //2-4查询次日留存
        Integer numRetention1d = logMapper.findByTodayAndYesterday(today, yesterday);

        //保存到每日分析表
        //查一下，要是null的
        QueryWrapper<AnalysisByDay> qw = new QueryWrapper<>();
        qw.eq("record_Date", today);
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(qw);
        if (analysisByDay == null) {
            //新增
            analysisByDay = new AnalysisByDay();
            analysisByDay.setRecordDate(new Date());
            analysisByDay.setNumRegistered(numRegistered);
            analysisByDay.setNumActive(numActive);
            analysisByDay.setNumLogin(numLogin);
            analysisByDay.setNumRetention1d(numRetention1d);
            //新增插入
            analysisByDayMapper.insert(analysisByDay);

        }else {
            analysisByDay.setNumRegistered(numRegistered);
            analysisByDay.setNumActive(numActive);
            analysisByDay.setNumLogin(numLogin);
            analysisByDay.setNumRetention1d(numRetention1d);
            //修改
            analysisByDayMapper.updateById(analysisByDay);
        }


    }

    @Override
    public AnalysisSummaryVo findAnalysisSummaryVo() {
        //准备基本数据
        //1-1 今天
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        //1-2 昨天
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();
        //1-3 前7天
        String lastWeek = DateUtil.offsetWeek(new Date(), -1).toDateStr();
        //1-4 前30天
        String lastMonth = DateUtil.offsetDay(new Date(), -30).toDateStr();
        //1-5 今日分析数据
        AnalysisByDay todayAnalysisByDay = analysisByDayMapper.selectOne(new QueryWrapper<AnalysisByDay>().eq("record_date", today));
        //1-6 昨日分析数据
        AnalysisByDay yesterdayAnalysisByDay = analysisByDayMapper.selectOne(new QueryWrapper<AnalysisByDay>().eq("record_date", yesterday) );

        //准备封装vo的9项数据
        //2-1 累计用户数
        Long cumulativeUsers = analysisByDayMapper.findCumulativeUsers();
        //2-2 过去30天活跃用户数
        Long activePassMonth = analysisByDayMapper.findActivePassCondition(lastMonth, today);
        //2-3 过去7天活跃用户
        Long activePassWeek = analysisByDayMapper.findActivePassCondition(lastWeek, today);
        //2-4 今日新增用户数量
        Long newUsersToday = todayAnalysisByDay.getNumRegistered().longValue();
        //2-5 今日新增用户涨跌率，单位百分数，正数为涨，负数为跌   BigDecimal : 商业数字格式
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(todayAnalysisByDay.getNumRegistered(), yesterdayAnalysisByDay.getNumRegistered());
        //2-6 今日登录次数
        Long loginTimesToday = todayAnalysisByDay.getNumLogin().longValue();
        //2-7 今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(todayAnalysisByDay.getNumLogin(), yesterdayAnalysisByDay.getNumLogin());
        //2-8  今日活跃用户数量
        Long activeUsersToday = todayAnalysisByDay.getNumActive().longValue();
        //2-9 今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(todayAnalysisByDay.getNumActive(), yesterdayAnalysisByDay.getNumActive());

        //封装vo
        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        vo.setCumulativeUsers(cumulativeUsers);
        vo.setActivePassMonth(activePassMonth);
        vo.setActivePassWeek(activePassWeek);
        vo.setNewUsersToday(newUsersToday);
        vo.setNewUsersTodayRate(newUsersTodayRate);
        vo.setLoginTimesToday(loginTimesToday);
        vo.setLoginTimesTodayRate(loginTimesTodayRate);
        vo.setActiveUsersToday(activeUsersToday);
        vo.setActiveUsersTodayRate(activeUsersTodayRate);

        return vo;
    }
}
